/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.unavarra.mti.iws.logic.undoredo;

import es.unavarra.mti.iws.logic.properties.HasProperties;
import es.unavarra.mti.iws.logic.properties.KProperty;
import es.unavarra.mti.iws.logic.properties.PropertiesTableModel;
import java.util.List;

/**
 *
 * @author Asier
 */
public class SetProperty implements UndoRedoOperation{
    private PropertiesTableModel table;
    private KProperty property;
    private HasProperties[] objects;
    private Object[] oldValues;
    private Object newValue;

    
    public SetProperty(PropertiesTableModel table,List<HasProperties> objs, KProperty property, Object newValue) {
        this.table = table;
        synchronized(objs){
            int size = objs.size();
            objects = new HasProperties[size];
            oldValues = new Object[size];
            int i = 0;
            for(HasProperties obj : objs){
                objects[i] = obj;
                oldValues[i] = property.getProperty(obj);
                i++;
            }
        }
        this.property = property;
        this.newValue = newValue;
    }



    public void undo() {
        int i = 0;
        for(HasProperties obj : objects){
            property.setProperty(obj, oldValues[i]);
            i++;
        }
        table.refresh();
    }

    public void redo() {
        for(HasProperties obj : objects){
            property.setProperty(obj, newValue);
        }
        table.refresh();
    }

}
